﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Sizebox.CharacterEditor
{
	// Token: 0x02000247 RID: 583
	public class MaterialControlGui : MonoBehaviour
	{
		// Token: 0x06000C90 RID: 3216 RVA: 0x00065DE8 File Offset: 0x00063FE8
		private void Awake()
		{
			this.PrepareShaderSelection();
			this.colorPicker.onValueChanged.AddListener(new UnityAction<Color>(this.OnColorChanged));
			this.colorSelect.onValueChanged.AddListener(new UnityAction<int>(this.OnTargetColorSelect));
			this.resetButton.onClick.AddListener(new UnityAction(this.OnReset));
		}

		// Token: 0x06000C91 RID: 3217 RVA: 0x00065E50 File Offset: 0x00064050
		private void PrepareShaderSelection()
		{
			if (this.shaders.Count > 0)
			{
				List<Dropdown.OptionData> list = new List<Dropdown.OptionData>();
				foreach (Shader shader in this.shaders)
				{
					list.Add(new Dropdown.OptionData
					{
						text = shader.name
					});
				}
				this.shaderSelect.AddOptions(list);
			}
			this.shaderSelect.onValueChanged.AddListener(new UnityAction<int>(this.OnShaderSelect));
		}

		// Token: 0x06000C92 RID: 3218 RVA: 0x00065F00 File Offset: 0x00064100
		public void RegisterMaterial(MaterialEntryGui clickedMaterial)
		{
			if (!clickedMaterial)
			{
				return;
			}
			if (Input.GetKey(this.multiselectKey))
			{
				if (!this.selectedMaterials.Contains(clickedMaterial))
				{
					clickedMaterial.Select();
					this.selectedMaterials.Add(clickedMaterial);
				}
				else if (!clickedMaterial.GetComponentInChildren<MaterialControlGui>())
				{
					this.activeGui = clickedMaterial;
					this.OpenControls();
				}
			}
			else if (!Input.GetKey(this.deselectKey) && (this.selectedMaterials.Count > 1 || !this.selectedMaterials.Contains(clickedMaterial)))
			{
				this.ClearSelectedMaterials();
				this.activeGui = clickedMaterial;
				clickedMaterial.Select();
				this.selectedMaterials.Add(clickedMaterial);
				this.OpenControls();
			}
			else
			{
				clickedMaterial.Deselect();
				this.selectedMaterials.Remove(clickedMaterial);
			}
			if (this.selectedMaterials.Count <= 0)
			{
				base.gameObject.SetActive(false);
			}
			else if (!this.activeGui || !this.activeGui.Selected)
			{
				this.activeGui = this.selectedMaterials[this.selectedMaterials.Count - 1];
				this.OpenControls();
			}
		}

		// Token: 0x06000C93 RID: 3219 RVA: 0x00066048 File Offset: 0x00064248
		private void OpenControls()
		{
			base.transform.SetParent(this.activeGui.transform);
			base.gameObject.SetActive(true);
			this.PrepareShader();
		}

		// Token: 0x06000C94 RID: 3220 RVA: 0x00066074 File Offset: 0x00064274
		private void PrepareShader()
		{
			this.HandleExtraShaderOption();
			Shader shader = this.activeGui.Wrapper.Shader;
			if (this.shaders.Contains(shader))
			{
				this.shaderSelect.value = this.shaders.IndexOf(shader);
			}
			else
			{
				this.shaderSelect.value = this.shaderSelect.options.Count - 1;
			}
			this.PrepareColorOptions();
			this.UpdateControls();
		}

		// Token: 0x06000C95 RID: 3221 RVA: 0x000660F0 File Offset: 0x000642F0
		private void PrepareColorOptions()
		{
			this.colorSelect.options.Clear();
			if (this.activeGui == null)
			{
				return;
			}
			foreach (string text in this.colorPropertyNames)
			{
				if (this.activeGui.Wrapper.Material.HasProperty(text))
				{
					this.colorSelect.options.Add(new Dropdown.OptionData(text));
				}
			}
			if (this.colorSelect.options.Count == 0)
			{
				this.targetColor = null;
			}
			else
			{
				this.colorSelect.value = 0;
				this.OnTargetColorSelect(0);
			}
		}

		// Token: 0x06000C96 RID: 3222 RVA: 0x000661CC File Offset: 0x000643CC
		private void HandleExtraShaderOption()
		{
			if (this.extraOption != null)
			{
				this.shaderSelect.options.Remove(this.extraOption);
				this.extraOption = null;
				this.extraShader = null;
			}
			if (this.activeGui != null)
			{
				Material defaultMaterial = this.activeGui.Wrapper.DefaultMaterial;
				if (!this.shaders.Contains(defaultMaterial.shader))
				{
					this.extraShader = defaultMaterial.shader;
					this.extraOption = new Dropdown.OptionData(defaultMaterial.shader.name);
					this.shaderSelect.options.Add(this.extraOption);
				}
			}
		}

		// Token: 0x06000C97 RID: 3223 RVA: 0x0006627C File Offset: 0x0006447C
		private void UpdateControls()
		{
			this.UpdateColor();
			foreach (MaterialControlOption materialControlOption in this.controlOptions)
			{
				materialControlOption.ValidateOption(this.activeGui, this.selectedMaterials);
			}
		}

		// Token: 0x06000C98 RID: 3224 RVA: 0x000662EC File Offset: 0x000644EC
		private void UpdateColor()
		{
			this.colorPicker.CurrentColor = this.activeGui.Wrapper.Material.GetColor(this.targetColor);
		}

		// Token: 0x06000C99 RID: 3225 RVA: 0x00066314 File Offset: 0x00064514
		private void OnShaderSelect(int selection)
		{
			if (this.activeGui == null)
			{
				return;
			}
			Shader shader;
			if (selection < this.shaders.Count)
			{
				shader = this.shaders[selection];
			}
			else
			{
				shader = this.extraShader;
			}
			if (shader)
			{
				foreach (MaterialEntryGui materialEntryGui in this.selectedMaterials)
				{
					materialEntryGui.Wrapper.Shader = shader;
				}
			}
			this.PrepareShader();
		}

		// Token: 0x06000C9A RID: 3226 RVA: 0x000663C4 File Offset: 0x000645C4
		private void OnTargetColorSelect(int index)
		{
			this.targetColor = this.colorSelect.options[index].text;
			this.UpdateColor();
		}

		// Token: 0x06000C9B RID: 3227 RVA: 0x000663E8 File Offset: 0x000645E8
		private void OnColorChanged(Color color)
		{
			if (this.selectedMaterials == null || this.targetColor == null)
			{
				return;
			}
			foreach (MaterialEntryGui materialEntryGui in this.selectedMaterials)
			{
				if (materialEntryGui.Wrapper.Material.HasProperty(this.targetColor))
				{
					materialEntryGui.Wrapper.Material.SetColor(this.targetColor, color);
				}
			}
		}

		// Token: 0x06000C9C RID: 3228 RVA: 0x00066488 File Offset: 0x00064688
		private void OnReset()
		{
			foreach (MaterialEntryGui materialEntryGui in this.selectedMaterials)
			{
				materialEntryGui.Wrapper.ResetMaterial();
			}
			this.HandleExtraShaderOption();
			this.PrepareShader();
		}

		// Token: 0x06000C9D RID: 3229 RVA: 0x000664F4 File Offset: 0x000646F4
		private void OnDisable()
		{
			this.ClearSelectedMaterials();
			base.gameObject.SetActive(false);
		}

		// Token: 0x06000C9E RID: 3230 RVA: 0x00066508 File Offset: 0x00064708
		private void ClearSelectedMaterials()
		{
			this.activeGui = null;
			foreach (MaterialEntryGui materialEntryGui in this.selectedMaterials)
			{
				materialEntryGui.Deselect();
			}
			this.selectedMaterials.Clear();
		}

		// Token: 0x040011A1 RID: 4513
		[SerializeField]
		[Header("Required References")]
		private Button resetButton;

		// Token: 0x040011A2 RID: 4514
		[SerializeField]
		private ColorPicker colorPicker;

		// Token: 0x040011A3 RID: 4515
		[SerializeField]
		private Dropdown colorSelect;

		// Token: 0x040011A4 RID: 4516
		[SerializeField]
		private Dropdown shaderSelect;

		// Token: 0x040011A5 RID: 4517
		[Space]
		[SerializeField]
		private List<string> colorPropertyNames;

		// Token: 0x040011A6 RID: 4518
		[SerializeField]
		private List<Shader> shaders;

		// Token: 0x040011A7 RID: 4519
		[SerializeField]
		[Space]
		private List<MaterialControlOption> controlOptions;

		// Token: 0x040011A8 RID: 4520
		private MaterialEntryGui activeGui;

		// Token: 0x040011A9 RID: 4521
		private List<MaterialEntryGui> selectedMaterials = new List<MaterialEntryGui>();

		// Token: 0x040011AA RID: 4522
		private Shader extraShader;

		// Token: 0x040011AB RID: 4523
		private Dropdown.OptionData extraOption;

		// Token: 0x040011AC RID: 4524
		private string targetColor;

		// Token: 0x040011AD RID: 4525
		private KeyCode multiselectKey = KeyCode.LeftShift;

		// Token: 0x040011AE RID: 4526
		private KeyCode deselectKey = KeyCode.LeftControl;
	}
}
